﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.SQL.LiquidView;
using Algobox.JuicyLib.Apps.Yahoo;
using Algobox.JuicyLib.Apps.EODData;
using LiquidViewFXFetcher.Properties;
using Algobox.JuicyLib.MarketData.Structure;

namespace LiquidViewFXFetcher
{
    class Program
    {

        static void Main(string[] args)
        {
            DateTime date;
            string[] currencies = null;

            if (args.Length > 0)
            {
                if (args.Length == 2 && args[0] == "-D")
                {
                    date = DateTime.ParseExact(args[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
                    currencies = LiquidViewFXFetcher.Properties.Settings.Default.CurrenciesCSV.Split(',');
                }
                else if (args.Length == 4 && args[0] == "-D" && args[2] == "-C")
                {
                    date = DateTime.ParseExact(args[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
                    currencies = args[3].Split(',');
                }
                else
                {
                    Usage();
                    return;
                }
            }
            else
            {
                date = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));
                currencies = LiquidViewFXFetcher.Properties.Settings.Default.CurrenciesCSV.Split(',');
            }

            LiquidViewHelper.LiquidViewLinq = new LiquidViewLinqDataContext(Settings.Default.DBConString);

            if (currencies != null && currencies.Length < 2)
            {
                Console.WriteLine("\nPlease specify ISO currency symbols seperated by a comma in the FXFetcher.Config files 'CurrenciesCSV' field");
                Console.WriteLine("We can't use " + String.Join(",", currencies));

                if (args.Length == 0)
                    LiquidViewHelper.LogException("FXFetcher.exe", "Failed to load CurrenciesCSV values from FXFetcher.Config");

                return;
            }
            
            EODDataClient client = new EODDataClient(Settings.Default.EODDataUsername, Settings.Default.EODDataPassword);

            OHLC item;

            Dictionary<string, OHLC> items;

            if (!client.TryGetEndOfDay(EODDataClient.Exchanges.Forex, date, out items))
            {
                Console.WriteLine("No FX file found for {0:d}", date);

                if (args.Length == 0)                
                    LiquidViewHelper.LogException("FXFetcher.exe", String.Format("No FX file found for {0:d}", date));
                
                return;
            }

            foreach (string baseCCY in currencies)
            {
                foreach (string subjectCCY in currencies)
                {
                    // yes the order is correct, it is the wrong way around in LiquidView
                    if (items.TryGetValue(subjectCCY + baseCCY, out item))
                    {
                        if (LiquidViewHelper.TryImportFXrate(date, baseCCY, subjectCCY, item.Close))
                            Console.WriteLine("Success: {3:d} {0} from {1} {2}", baseCCY, subjectCCY, item.Close, date);
                        else
                            Console.WriteLine("Fail:    {3:d} {0} from {1} {2}", baseCCY, subjectCCY, item.Close, date);
                    }
                }
            }
            
        }

        private static void Usage()
        {
            Console.WriteLine("\nUsage: {0} [-D YYYYMMDD [-C GBP,EUR,USD,AUD...]]", Environment.CommandLine);
        }






    }
}
